﻿//-----------------------------------------------------------------------
// <copyright file="EndpointFactory.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace Microsoft.Live.Messenger
{
    using System;
    using System.Collections.Generic;

    internal sealed class EndpointFactory
    {
        private readonly Dictionary<MessengerEntity, Endpoint> endpoints = new Dictionary<MessengerEntity, Endpoint>();

        private readonly User user;

        public EndpointFactory(User user)
        {
            this.user = user;
        }

        public Endpoint FindOrCreate(object obj)
        {
            if (obj == null)
            {
                return null;
            }

            return FindOrCreate(new MessengerEntity(obj));
        }

        public Endpoint FindOrCreate(MessengerEntity entity)
        {
            Endpoint endpoint = null;

            if (this.endpoints.TryGetValue(entity, out endpoint))
            {
                return endpoint;
            }

            endpoint = new Endpoint(entity, this.user);
            this.endpoints.Add(entity, endpoint);
            return endpoint;
        }
    }
}